package view;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumnModel;
import javax.swing.text.MaskFormatter;

import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;

import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.text.ParseException;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JList;

import bean.Estado;
import clienteQuery.Cliente;
import controleCliente.ModelControleCliente;

import javax.swing.JComboBox;

public class ClienteJanela {


	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="25,19"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	
	private JButton jButtonSalvar = null;
	private JButton jButtonCancelar = null;
	private JButton jButtonExcluir = null;
	
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private ModelControleCliente modelControleCliente = null;
	
	private JLabel jLabelNome = null;
	private JLabel jLabelContato = null;
	private JLabel jLabelCnpf = null;
	private JLabel jLabelEmail = null;
	//private JLabel jLabelBanco = null;
	private JLabel jLabelTitulo = null;
	private JLabel jLabelTelefone = null;
	private JLabel jLabelFax = null;
	private JLabel jLabelEndereco = null;
	private JLabel jLabelRg = null;
	private JLabel jLabelBairro = null;
	private JLabel jLabelMunicipio = null;
	private JLabel jLabelUF = null;
	private JLabel jLabelCep = null;
	private JLabel jLabelCelular = null;
	
	private JTextField jTextFieldNome = null;
	private JTextField jTextFieldContato = null;
	private JTextField jTextFieldEndereco = null;
	private JTextField jTextFieldBairro = null;
	private JTextField jTextFieldMunicipio = null;
	private JTextField jTextFieldEmail = null;
	
	private JFormattedTextField jTextFieldCnpf = null;
	private JFormattedTextField jTextFieldTelefone = null;
	private JFormattedTextField jTextFieldFax = null;  
	private JFormattedTextField jTextFieldRg = null;
	private JFormattedTextField jTextFieldCep = null;
	private JFormattedTextField jTextFieldCelular = null;
	
	private JList jListBanco = null;
	
	private DefaultComboBoxModel ufComboModel = null;
	private JComboBox jComboBoxUf = null;
	

	
	public void criarJanela(ModelControleCliente modelControleCliente){
		//super.initialize(700, 630);
		this.modelControleCliente = modelControleCliente;
		getJFrame();
		jFrame.setVisible(true);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}


	/*************************************************************************/
	public JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setSize(new Dimension(1195, 515));
			jFrame.setContentPane(getJContentPane());
		}
		return jFrame;
	}

	/*************************************************************************/
	
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJButtonSalvar(), null);
			jContentPane.add(getJButtonCancelar(), null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getJButtonExcluir(), null);
			jContentPane.add(getJPanel2(), null);
		}
		return jContentPane;
	}

	/*************************************************************************/
	
	private JButton getJButtonSalvar() {
		if (jButtonSalvar == null) {
			jButtonSalvar = new JButton();
			jButtonSalvar.setBounds(new Rectangle(8, 5, 95, 30));
			jButtonSalvar.setText("Salvar");
		}
		return jButtonSalvar;
	}
	
	/*************************************************************************/
	
	private JButton getJButtonCancelar() {
		if (jButtonCancelar == null) {
			jButtonCancelar = new JButton();
			jButtonCancelar.setBounds(new Rectangle(103,5, 95, 30));
			jButtonCancelar.setText("Cancelar");
		}
		return jButtonCancelar;
	}

	/*************************************************************************/

	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(379, 40, 796, 435));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/*************************************************************************/

	public JTable getJTable() {
		if (jTable == null) {
			//jTable = new JTable();
			
			jTable.setModel(new ModelControleCliente());
			jTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			
		}
		return jTable;
	}

	/*************************************************************************/
	
	public JButton getJButtonExcluir() {
		if (jButtonExcluir == null) {
			jButtonExcluir = new JButton("Excluir");
			jButtonExcluir.setBounds(new Rectangle(198, 5, 95, 30));
			jButtonExcluir.setVisible(false);
		}
		return jButtonExcluir;
	}

	/*************************************************************************/
	
	private JPanel getJPanel2() {
		if (jPanel == null) {
			
			
			jLabelTitulo = new JLabel();
			jLabelTitulo.setBounds(new Rectangle(5, 3, 342, 22));
			jLabelTitulo.setText("CADASTRO / EDIÇÃO / REMOÇÃO DE CLIENTE");

			jLabelCelular = new JLabel();
			jLabelCelular.setBounds(new Rectangle(6, 313, 70, 20));
			jLabelCelular.setText("Celular:"); 
			
			jLabelCep = new JLabel();
			jLabelCep.setBounds(new Rectangle(6, 238, 70, 20));
			jLabelCep.setText("CEP:"); 
			
			jLabelUF = new JLabel();
			jLabelUF.setBounds(new Rectangle(6, 213, 70, 20));
			jLabelUF.setText("UF:"); 
			
			jLabelMunicipio = new JLabel();
			jLabelMunicipio.setBounds(new Rectangle(6, 188, 70, 20));
			jLabelMunicipio.setText("Município:"); 

			jLabelBairro = new JLabel();
			jLabelBairro.setBounds(new Rectangle(6, 163, 70, 20));
			jLabelBairro.setText("Bairro:"); 
			
			jLabelEndereco = new JLabel();
			jLabelEndereco.setBounds(new Rectangle(6, 138, 70, 20));
			jLabelEndereco.setText("Endereço:"); 
			
			jLabelRg = new JLabel();
			jLabelRg.setBounds(new Rectangle(6, 113, 70, 20));
			jLabelRg.setText("IE/RG:"); 
			
			jLabelFax = new JLabel();
			jLabelFax.setBounds(new Rectangle(7, 288, 70, 20));
			jLabelFax.setText("Fax:");
			
			jLabelTelefone = new JLabel();
			jLabelTelefone.setBounds(new Rectangle(7, 263, 70, 20));
			jLabelTelefone.setText("Telefone:");
			
			/*jLabelBanco = new JLabel();
			jLabelBanco.setBounds(new Rectangle(7, 363, 70, 20));
			jLabelBanco.setText("Banco:");*/
						
			jLabelEmail = new JLabel();
			jLabelEmail.setBounds(new Rectangle(7, 338, 70, 20));
			jLabelEmail.setText("E-mail:");
			
			jLabelCnpf = new JLabel();
			jLabelCnpf.setBounds(new Rectangle(6, 88, 70, 20));
			jLabelCnpf.setText("CPF/CNPJ:");
			
			jLabelContato = new JLabel();
			jLabelContato.setBounds(new Rectangle(6, 63, 70, 20));
			jLabelContato.setText("Contato:");
			
			jLabelNome = new JLabel();
			jLabelNome.setBounds(new Rectangle(6, 38, 70, 20));
			jLabelNome.setText("* Nome:");
			
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(8, 40, 355, 435));
			jPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			
			jPanel.add(jLabelNome, null);
			jPanel.add(jLabelContato, null);
			jPanel.add(jLabelCnpf, null);
			jPanel.add(jLabelEmail, null);
			//jPanel.add(jLabelBanco, null);
			jPanel.add(jLabelTitulo, null);
			jPanel.add(jLabelTelefone, null);
			jPanel.add(jLabelFax, null);
			jPanel.add(jLabelEndereco, null);
			jPanel.add(jLabelRg, null);
			jPanel.add(jLabelBairro, null);
			jPanel.add(jLabelMunicipio, null);
			jPanel.add(jLabelUF, null);
			jPanel.add(jLabelCep, null);
			jPanel.add(jLabelCelular, null);
			
			jPanel.add(getJTextFieldNome(), null);
			jPanel.add(getJTextFieldContato(), null);
			jPanel.add(getJTextFieldCnpf(), null);
			jPanel.add(getJTextFieldEmail(), null);
			//jPanel.add(getJListBanco(), null);
			jPanel.add(getJTextFieldTelefone(), null);
			jPanel.add(getJTextFieldFax(), null);
			jPanel.add(getJTextFieldRg(), null);
			jPanel.add(getJTextFieldEndereco(), null);
			jPanel.add(getJTextFieldBairro(), null);
			jPanel.add(getJTextFieldMunicipio(), null);
			jPanel.add(getJTextFieldCep(), null);
			jPanel.add(getJTextFieldCelular(), null);
			jPanel.add(getJComboBoxUf(), null);
		}
		return jPanel;
	}

	/*************************************************************************/
	
	public JTextField getJTextFieldNome() {
		if (jTextFieldNome == null) {
			jTextFieldNome = new JTextField();
			jTextFieldNome.setDocument(new modeloAux.FixedLengthDocument(70));
			jTextFieldNome.setBounds(new Rectangle(84, 38, 264, 20));
		}
		return jTextFieldNome;
	}

	/*************************************************************************/
	
	public JTextField getJTextFieldContato() {
		if (jTextFieldContato == null) {
			jTextFieldContato = new JTextField();
			jTextFieldContato.setDocument(new modeloAux.FixedLengthDocument(70));
			jTextFieldContato.setBounds(new Rectangle(84, 63, 264, 20));
		}
		return jTextFieldContato;
	}

	/*************************************************************************/
	
	public JFormattedTextField getJTextFieldCnpf() {
		if (jTextFieldCnpf == null) {
			try {
				jTextFieldCnpf = new JFormattedTextField(new MaskFormatter("################"));
			} catch (ParseException e) {}
			
			//jTextFieldCnpf.setDocument(new modeloAux.FixedLengthDocument(16));
			jTextFieldCnpf.setBounds(new Rectangle(84, 88, 160, 20));
			
		}
		return jTextFieldCnpf;
	}

	
	
	/*************************************************************************/
	
	public JTextField getJTextFieldEmail() {
		if (jTextFieldEmail == null) {
			jTextFieldEmail = new JTextField();
			jTextFieldEmail.setDocument(new modeloAux.FixedLengthDocument(70));
			jTextFieldEmail.setBounds(new Rectangle(84, 338, 264, 20));
		}
		return jTextFieldEmail;
	}

	/*************************************************************************
	
	public JList getJListBanco() {
		if (jListBanco == null) {
			jListBanco = new JList();
			jListBanco.setBounds(new Rectangle(84, 363, 225, 20)); 
		}
		return jListBanco;
	}

	/*************************************************************************/
	
	public JFormattedTextField getJTextFieldTelefone() {
		if (jTextFieldTelefone == null) {
			try {
				jTextFieldTelefone = new JFormattedTextField(new MaskFormatter("(##) #### ####"));
			} catch (ParseException e) {}
			
			//jTextFieldTelefone.setDocument(new modeloAux.FixedLengthDocument(12));
			jTextFieldTelefone.setBounds(new Rectangle(84, 263, 150, 20));
		}
		return jTextFieldTelefone;
	}

	/*************************************************************************/
	
	public JFormattedTextField getJTextFieldFax() {
		if (jTextFieldFax == null) {
			try {
				jTextFieldFax = new JFormattedTextField(new MaskFormatter("(##) #### ####"));
			} catch (ParseException e) {}
			
			//jTextFieldFax.setDocument(new modeloAux.FixedLengthDocument(12));
			jTextFieldFax.setBounds(new Rectangle(84, 288, 150, 20));
		}
		return jTextFieldFax;
	}
	
	/*************************************************************************/

	public JFormattedTextField getJTextFieldRg() {
		if (jTextFieldRg == null) {
			try {
				jTextFieldRg = new JFormattedTextField(new MaskFormatter("##############"));
			} catch (ParseException e) {}
		//	jTextFieldRg.setDocument(new modeloAux.FixedLengthDocument(14));
			jTextFieldRg.setBounds(new Rectangle(84, 113,  140, 20));
		}
		return jTextFieldRg;
	}

	/*************************************************************************/
	
	public JTextField getJTextFieldEndereco() {
		if (jTextFieldEndereco == null) {
			jTextFieldEndereco = new JTextField();
			jTextFieldEndereco.setDocument(new modeloAux.FixedLengthDocument(70));
			jTextFieldEndereco.setBounds(new Rectangle(84, 138, 264, 20));
		}
		return jTextFieldEndereco;
	}

	/*************************************************************************/
	
	public JTextField getJTextFieldBairro() {
		if (jTextFieldBairro == null) {
			jTextFieldBairro = new JTextField();
			jTextFieldBairro.setDocument(new modeloAux.FixedLengthDocument(35));
			jTextFieldBairro.setBounds(new Rectangle(84, 163,  264, 20));
		}
		return jTextFieldBairro;
	}
	
	/*************************************************************************/
	
	public JTextField getJTextFieldMunicipio() {
		if (jTextFieldMunicipio == null) {
			jTextFieldMunicipio = new JTextField();
			jTextFieldMunicipio.setDocument(new modeloAux.FixedLengthDocument(35));
			jTextFieldMunicipio.setBounds(new Rectangle(84, 188, 264, 20));
		}
		return jTextFieldMunicipio;
	}

	/*************************************************************************/
	
	public JComboBox getJComboBoxUf() {
		if (jComboBoxUf == null) {
			jComboBoxUf = new JComboBox();
			jComboBoxUf.setBounds(new Rectangle(84, 213, 60, 20));			
		}
		return jComboBoxUf;
	}

	/*************************************************************************/
	
	public JFormattedTextField getJTextFieldCep() {
		if (jTextFieldCep == null) {
			try {
				jTextFieldCep = new JFormattedTextField(new MaskFormatter("#####-###"));
			
			} catch (ParseException e) {}
			
			//jTextFieldCep.setDocument(new modeloAux.FixedLengthDocument(10));
			jTextFieldCep.setBounds(new Rectangle(84, 238, 100, 20));
		}
		return jTextFieldCep;
	}

	/*************************************************************************/
	
	public JFormattedTextField getJTextFieldCelular() {
		if (jTextFieldCelular == null) {
			try {
				jTextFieldCelular = new JFormattedTextField(new MaskFormatter("(##) #### ####"));
			} catch (ParseException e) {}
			
			//jTextFieldCelular.setDocument(new modeloAux.FixedLengthDocument(12));
			jTextFieldCelular.setBounds(new Rectangle(84, 313, 150, 20));
		}
		return jTextFieldCelular;
	}
	/*fim do jframe*/
	
	/*************************************************************************/
	/*************************************************************************/
	
	public void limparFormulario(){
		getJTextFieldCep().setText("");
		getJTextFieldCelular().setText("");
		getJTextFieldBairro().setText("");
		getJTextFieldCnpf().setText("");
		getJTextFieldContato().setText("");
		getJTextFieldEmail().setText("");
		getJTextFieldEndereco().setText("");
		getJTextFieldFax().setText("");
		getJTextFieldMunicipio().setText("");
		getJTextFieldNome().setText("");
		getJTextFieldRg().setText("");
		getJTextFieldTelefone().setText("");
	}
	
	public void preencheFormulario(Cliente cliente){
		getJTextFieldCep().setText(cliente.getCep());
		getJTextFieldCelular().setText(cliente.getCelular());
		getJTextFieldBairro().setText(cliente.getBairro());
		getJTextFieldCnpf().setText(cliente.getCnpjCpf());
		getJTextFieldContato().setText(cliente.getContato());
		getJTextFieldEmail().setText(cliente.getEmail());
		getJTextFieldEndereco().setText(cliente.getEndereco());
		getJTextFieldFax().setText(cliente.getFax());
		getJTextFieldMunicipio().setText(cliente.getMunicipio());
		getJTextFieldRg().setText(cliente.getRgIe());
		getJTextFieldTelefone().setText(cliente.getTelefone());
		getJTextFieldNome().setText(cliente.getNome());
		
		int tam = getJComboBoxUf().getItemCount();
		for(int i = 0; i < tam; i ++){
			if(((Estado)getJComboBoxUf().getItemAt(i)).getId() == cliente.getEstado().getId()){
				getJComboBoxUf().setSelectedIndex(i);
				break;
			}
		}
		
	}

	public void preencheListaUf(DefaultComboBoxModel model){
		this.ufComboModel = model;
		getJComboBoxUf().setModel(this.ufComboModel);
	}
	
	public void setListenerBotaoSalvar(ActionListener listener) {
		getJButtonSalvar().addActionListener(listener);
		
	}

	public void setListenerBotaoCancelar(ActionListener listener) {
		getJButtonCancelar().addActionListener(listener);		
		
	}

	public void setListenerBotaoExcluir(ActionListener listener) {
		getJButtonExcluir().addActionListener(listener);	
		
	}
	
	public void setListenerCliqueDuplo(MouseAdapter evt) {
		getJTable().addMouseListener(evt);
				
	}
	
	public ModelControleCliente getPlanosTable(){
		return (ModelControleCliente) jTable.getModel();
	}
	
	public int linhaSelecionada(){
		return this.jTable.getSelectedRow();
	}
	
	public void refreshJtable(ModelControleCliente model){
		//this.modelControleCliente.setVetorClientes(model.getVetorClientes());
		//getJTable().tableChanged(new TableModelEvent(modelControleCliente));
		//modelControleCliente.fireTableChanged(new TableModelEvent(modelControleCliente));
		getJTable().repaint();
	}

	public void escondeColID(int col){
		TableColumnModel  colunaModelo = getJTable().getColumnModel();      
		colunaModelo.getColumn(col).setMinWidth(0);     
		colunaModelo.getColumn(col).setPreferredWidth(0);  
		colunaModelo.getColumn(col).setMaxWidth(0); 	
	}

	public void setListenerSoNumero(FocusListener listener) {
		//getJTextFieldCelular().addFocusListener(listener);
		//getJTextFieldTelefone().addFocusListener(listener);
		//getJTextFieldFax().addFocusListener(listener);
		//getJTextFieldCep().addFocusListener(listener);
		//getJTextFieldCnpf().addFocusListener(listener);
		//getJTextFieldRg().addFocusListener(listener);
	}
	
	
	
}
